﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartHotel.CheckIns.Dto;
using SmartHotel.Dto;
using SmartHotel.HouseRoom.Dto;
using SmartHotel.Rooms;
using SmartHotel.Rooms.Dto;
using SmartHotel.Users.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Yitter.IdGenerator;

namespace SmartHotel.CheckIns
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppCheckInService : ApplicationService, ISmartHotelAppCheckInService
    {
        private readonly IRepository<SmartHotel_HouseState, int> repository;
        private readonly IRepository<SmartHotel_RoomNumberTable, int> repositoryRoom;
        private readonly IRepository<SmartHotel_HouseRoom, int> repositoryhouseroom;

        private readonly IRepository<SmartHotel_HouseTable, int> repositoryhouse;
        private readonly IRepository<SmartHotel_UserTable, int> repositoryUser;

        public SmartHotelAppCheckInService(IRepository<SmartHotel_HouseState, int> repository, IRepository<SmartHotel_RoomNumberTable, int> repositoryRoom, IRepository<SmartHotel_HouseRoom, int> repositoryhouseroom, IRepository<SmartHotel_HouseTable, int> repositoryhouse, IRepository<SmartHotel_UserTable, int> repositoryUser) 
        {
            this.repository = repository;
            this.repositoryRoom = repositoryRoom;
            this.repositoryhouseroom = repositoryhouseroom;
            this.repositoryhouse = repositoryhouse;
            this.repositoryUser = repositoryUser;
        }
        #region 基础
        /// <summary>
        /// 添加房态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<List<CreateUpdateCheckIn>>> PostAddChenk(List<CreateUpdateCheckIn> model)
        {
            var list = ObjectMapper.Map<List<CreateUpdateCheckIn>, List<SmartHotel_HouseState>>(model);
            await repository.InsertManyAsync(list);
            return new ResultDto<List<CreateUpdateCheckIn>> { Code = Result.Success, Message = "预订成功" };
        }
        /// <summary>
        /// 通过ID查询到用户的信息
        /// </summary>
        /// <param name="userPhone"></param>
        /// <returns></returns>
        public async Task<ResultDto<UserDto>> GetUserPhone(string userPhone)
        {
            var data = await repositoryUser.GetListAsync(a => a.UserPhone == userPhone);
            if (data == null)
            {
                return new ResultDto<UserDto> { Code = Result.Failed, Data = null, Message = "查询失败" };
            }
            else
            {
                return new ResultDto<UserDto> { Code = Result.Success, Data = data, Message = "查询成功" };
            }

        }
        /// <summary>
        /// 房号房型显示
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<HouseRoomDto>> GetHuoseRoom()
        {
            var roomlist = await repositoryRoom.GetListAsync();
            var houselist = await repositoryhouse.GetListAsync();

            var data = (from r in roomlist
                        join h in houselist
                        on r.HouseId equals h.Id
                        select h).Distinct();

            foreach (var item in data.ToList())
            {
                item.RoomNumInt = (from b in roomlist
                                   where item.Id == b.HouseId
                                   select b).Distinct().Count();
            }

            return new ResultDto<HouseRoomDto> { Code = Result.Success, Message = "显示", Data = data };

        }
        /// <summary>
        /// 分组显示
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<RoomGroupDto>>> GetHouseRoomGroup(int houseId = 0, int houseRoomState = 0, int roomRoomState = 0)
        {
            var roomlist = await repositoryRoom.GetListAsync();
            var houseroomlist = await repositoryhouseroom.GetListAsync();
            var houselist = await repositoryhouse.GetListAsync();

            var query = (from hr in houseroomlist
                         join r in roomlist
                         on hr.RoomId equals r.Id
                         join h in houselist
                         on hr.RoomNumberId equals h.Id
                         select new HouseRooml()
                         {
                             Id = hr.Id,
                             RoomId = r.Id,
                             RoomNumberId = h.Id,
                             HouseId = h.Id,
                             RowHouse = hr.RowHouse,
                             RoomSort = r.RoomSort,
                             RoomRemark = r.RoomRemark,
                             RoomState = r.RoomState,
                             HouseState = r.HouseState,
                             RoomNumber = r.RoomNumber,
                             HouseName = h.HouseName,
                             HousePrice = h.HousePrice,
                             HouseSecurity = h.HouseSecurity,
                             HouseArea = h.HouseArea,
                             HousePeople = h.HousePeople,
                             HousePicture = h.HousePicture,
                             HouseType = h.HouseType,
                             ExtraBed = h.ExtraBed,
                             ExtraBedNumber = h.ExtraBedNumber,
                             HouseRoomState = (HouseTypeEnum)hr.HouseRoomState,
                             RoomRoomState = (MoreTypeEnum)hr.RoomRoomState,
                             HouseRuZhu=(SmartHotel_OrderState)hr.OrderState,
                             AveragePrice=h.AveragePrice,
                         }).AsQueryable();
            query = query.WhereIf(houseId != 0, a => a.HouseId == houseId);
            query = query.WhereIf(houseRoomState != 0, a => a.HouseRoomState == (HouseTypeEnum)houseRoomState);
            query = query.WhereIf(roomRoomState != 0, a => a.RoomRoomState == (MoreTypeEnum)roomRoomState);

            var groupedQuery = query.GroupBy(x => x.HouseName).Select(group =>
            new RoomGroupDto
            {
                HouseName = group.Key,
                HouseRoomList = group.ToList()
            }).ToList();

            return new ResultDto<List<RoomGroupDto>> { Code = Result.Success, Message = "显示", Data = groupedQuery };


        }
        /// <summary>
        /// 房间状态
        /// </summary>
        /// <returns></returns>
        public ResultDto<object> GetHouseTypeEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<HouseTypeEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return new ResultDto<object> { Code = Result.Success, Message = "显示", Data = list };
        }
        /// <summary>
        /// 房间是否整洁
        /// </summary>
        /// <returns></returns>
        public ResultDto<object> GetMoreTypeEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<MoreTypeEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return new ResultDto<object> { Code = Result.Success, Message = "显示", Data = list };
        }
        public async Task<ResultDto<bool>> UpdateStateGengduo(int housenumid, int stateid)
        {
            var list = await repositoryhouseroom.FindAsync(housenumid);
            if (stateid == 1)
            {
                list.RoomRoomState = (MoreTypeEnum)1;
            }
            if (stateid == 2)
            {
                list.RoomRoomState = (MoreTypeEnum)2;
            }
            if (stateid == 3)
            {
                list.RoomRoomState = (MoreTypeEnum)3;
            }
            await repositoryhouseroom.UpdateAsync(list);
            return new ResultDto<bool> { Code = Result.Success, Data = 200, Message = "更改成功" };
        }
        #endregion

        #region 附加小功能
        /// <summary>
        /// 雪花ID
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public ResultDto<string> GetSnowId(int count = 1)
        {
            List<object> list = new List<object>();
            for (int i = 0; i < count; i++)
            {
                long newId = YitIdHelper.NextId();
                list.Add(newId);
            }

            return new ResultDto<string> { Code = Result.Success, Data = list };
        }
        public ResultDto<object> GetDateWeekList()
        {
            List<object> list = new List<object>();
            DateTime sDate = DateTime.Now;
            int num = Convert.ToInt32(sDate.DayOfWeek);
            if (num == 0)
            {
                num = 7;
            }
            sDate = sDate.AddDays(1 - num);
            list.Add(new
            {
                Week1 = sDate.ToString("yyyy-MM-dd"),
                Week2 = sDate.AddDays(1).ToString("yyyy-MM-dd"),
                Week3 = sDate.AddDays(2).ToString("yyyy-MM-dd"),
                Week4 = sDate.AddDays(3).ToString("yyyy-MM-dd"),
                Week5 = sDate.AddDays(4).ToString("yyyy-MM-dd"),
                Week6 = sDate.AddDays(5).ToString("yyyy-MM-dd"),
                Week7 = sDate.AddDays(6).ToString("yyyy-MM-dd"),
            });
            return new ResultDto<object> { Code = Result.Success, Data = list };
        }
        //public ResultDto<int> Update
        #endregion

    }
}
